package danran.hash;

import java.util.HashSet;
import java.util.Set;

/**
 * @Classname LongestConsecutive
 * @Description TODO
 * @Date 2021/9/29 11:00
 * @Created by ASUS
 */
public class LongestConsecutive {

    public static void main(String[] args) {
        System.out.println(new LongestConsecutive().longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
    }

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int ans = 0, tmp;
        for (Integer n : set) {
            if (set.contains(n - 1)) continue;
            int cur = n;
            tmp = 0;
            while (set.contains(cur)) {
                tmp++;
                cur++;
            }
            ans = Math.max(ans, tmp);
        }
        return ans;
    }
}
